﻿//-----------------------------------------------------------------------
// <summary>
//  A module in Prism is a logical unit in your application.
// </summary>
// <copyright file="NewsArticleViewModel.cs" company="DSSoftLogic">
//     Copyright@ DSSoftLogic 2011, Non-Commercial. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Property.Silverlight.ViewModelEx
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Diagnostics.CodeAnalysis;

    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.Prism.ViewModel;
    using Property.Silverlight.Model;
    using Property.Silverlight.Model.Services;

    /// <summary>
    /// This NewsArticleViewModel is testing the StyleCop rules and how to use them.
    /// http://www.johnpapa.net/simple-viewmodel-locator-for-mvvm-the-patients-have-left-the-asylum
    /// 
    /// </summary>
    [Export(typeof(NewsArticleViewModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class NewsArticleViewModel : NotificationObject
    {
        /// <summary>
        /// newsFeedService field.
        /// </summary>
        private readonly INewsFeedService newsFeedService;

        /// <summary>
        /// _companySymbol field.
        /// </summary>
        private string companySymbol;

        /// <summary>
        /// articles field.
        /// </summary>
        private IList<NewsArticle> articles;

        /// <summary>
        /// regionManager field.
        /// </summary>
        private readonly IRegionManager regionManager;

        /// <summary>
        /// selectedArticle field.
        /// </summary>
        private NewsArticle selectedArticle;

        /// <summary>
        /// Initializes a new instance of the <see cref="NewsArticleViewModel"/> class.
        /// </summary>
        [ImportingConstructor]
        public NewsArticleViewModel(INewsFeedService newsFeedService, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            if (newsFeedService == null)
            {
                throw new ArgumentNullException("newsFeedService");
            }

            if (regionManager == null)
            {
                throw new ArgumentNullException("regionManager");
            }

            this.newsFeedService = newsFeedService;
            this.regionManager = regionManager;
            this.CompanySymbol = companySymbol;
            this.Articles = newsFeedService.GetNews("STOCK0");
            ////this.NewsArticleView.NewsArticleViewModel = (INewsArticleView)this;
            ////this.ShowArticleList();
            eventAggregator.GetEvent<TickerSymbolSelectedEvent>();////.Subscribe(OnTickerSymbolSelected, ThreadOption.UIThread);
        }

        /// <summary>
        /// Gets or sets the NewsArticleView.
        /// </summary>
        ////public INewsArticleView NewsArticleView { get; set; }

        /// <summary>
        /// Gets or sets CompanySymbol.
        /// </summary>
        public string CompanySymbol
        {
            get
            {
                return this.companySymbol;
            }

            set
            {
                if (this.companySymbol != value)
                {
                    this.companySymbol = value;
                    this.RaisePropertyChanged(() => this.CompanySymbol);
                    this.OnCompanySymbolChanged();
                }
            }
        }

        /// <summary>
        /// Gets or sets SelectedArticle.
        /// </summary>
        public NewsArticle SelectedArticle
        {
            get
            {
                return this.selectedArticle;
            }

            set
            {
                if (this.selectedArticle != value)
                {
                    this.selectedArticle = value;
                    this.RaisePropertyChanged(() => this.SelectedArticle);
                }
            }
        }

        /// <summary>
        /// Gets or set Articles.
        /// </summary>
        public IList<NewsArticle> Articles
        {
            get
            {
                return this.articles;
            }

            set
            {
                if (this.articles != value)
                {
                    this.articles = value;
                    this.RaisePropertyChanged(() => this.Articles);
                }
            }
        }

        /// <summary>
        /// The on ticker symbol selected.
        /// </summary>
        /// <param name="companySymbol">
        /// The company symbol.
        /// </param>
        public void OnTickerSymbolSelected(string companySymbol)
        {
            this.CompanySymbol = companySymbol;
        }

        /// <summary>
        /// OnCompanySymbolChanged method called by CompanySymbol.
        /// </summary>
        private void OnCompanySymbolChanged()
        {
            this.Articles = this.newsFeedService.GetNews(this.companySymbol);
        }


        /// <summary>
        /// ShowArticleList method.
        /// </summary>
        private void ShowArticleList()
        {
            this.SelectedArticle = null;
        }
    }

    /// <summary>
    /// The ticker symbol selected event.
    /// </summary>
    [SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "Reviewed. Suppression is OK here.")]
    public class TickerSymbolSelectedEvent : CompositePresentationEvent<string>
    {
    }
}
